<?php
class Menu extends MenuAppModel {
	var $useTable = 'sys_menus';
	public $actsAs = array('Pagination');
	public $tree = array();
	var $recursive = -1;
	
	public $validate = array(
		'title' => array(
			'rule' => 'notEmpty',
			'message'  => '请输入名称'
		),
		'weight' => array(
			'rule' => 'notEmpty',
			'message'  => '权重不能为空'
		)
	);
	
	public $hasMany = array(
		'SonMenu'=>array(
			'className'  => 'Menu',
			'foreignKey' => 'parent_id',
            'conditions' => array('enable' => 1),
            'order'      => 'sort ASC'
		)
	);
	/**
	 * 分页条件
	 * @Date 2012-11-30
	 * @return Array
	 */
	function pageParam($action){
		return array(
			"colNames"=>array(
				'ID','名称','plugin','Controller','Action','权重','排序','启用','操作'
			),
			"colModel"=>array(
				array("name"=>'Menu.id',"width"=>'50',"sortable"=>false,"align"=>"center"),
				array("name"=>'Menu.title',"width"=>"200","sortable"=>false),
				array("name"=>'Menu.plugin',"width"=>"100","sortable"=>false),
				array("name"=>'Menu.Controller',"width"=>"100","sortable"=>false),
				array("name"=>'Menu.Action',"width"=>"100","sortable"=>false),
				array("name"=>'Menu.weight',"width"=>"100","sortable"=>false),
				array("name"=>'Menu.sort',"width"=>"100","sortable"=>false,"align"=>"center"),
				array("name"=>'Menu.enable',"width"=>"50","sortable"=>false,"align"=>"center","formatter"=>"checkbox"),
				array("name"=>'act',"width"=>"200","align"=>"center","sortable"=>false)
			),
			"ExpandColumn"=>"Menu.title",
			"ajax"=>true,
			"action"=>$action
		);
	}
	
	/**
	 * 获取菜单
	 * ext cloum
	 * level/parent/isLeaf/expanded/loaded/icon
	 * 
	 * @Date 2012-11-30
	 * @param $parentId 上级id
	 * @param $level 层级
	 * @return
	 */
	function ajaxTreeCategory($parentId,$level){
	
		$t = $this->findCategory($parentId,'all');
		$leafTree = array();
		$row = array();
	
		foreach($t as $k => $v){
			array_push($leafTree,$v['Menu']['id']);
			array_push($leafTree,$v['Menu']['title']);
			array_push($leafTree,$v['Menu']['plugin']);
			array_push($leafTree,$v['Menu']['controller']);
			array_push($leafTree,$v['Menu']['action']);
			array_push($leafTree,$v['Menu']['weight']);
			array_push($leafTree,$v['Menu']['sort']);
			array_push($leafTree,$v['Menu']['enable']);
			array_push($leafTree,"");
	
			//level
			array_push($leafTree,$level);
	
			//parent
			array_push($leafTree,$parentId);
	
			//isLeaf
			$hasSon = $this->findSonNodel($v['Menu']['id'],'all');
			if($hasSon){
				array_push($leafTree,false);
			}else{
				array_push($leafTree,true);
			}
	
			//expanded
			if($v['Menu']['is_open']==1){
				$treeLoaded = true;
				array_push($leafTree,true);
			}else{
				$treeLoaded = false;
				array_push($leafTree,false);
			}
	
			//loaded
			array_push($leafTree,$treeLoaded);
	
			$row['id'] = $v['Menu']['id'];
			$row['cell'] = $leafTree;
	
			$leafTree = array();
	
			if(sizeof($row)>0){
				array_push($this->tree,$row);
			}
			
			if($v['Menu']['is_open']==1){
				$this->ajaxTreeCategory($v['Menu']['id'],$level+1);
			}
		}
		return $this->tree;
	}
	
	function findCategory($id = 0,$type = 'enable') {
		$conditions = array(
			'conditions' => array('Menu.parent_id'=>$id),
			'order' => array('Menu.sort ASC')
		);
		if($type == 'enable'){
			$conditions['conditions']['Menu.enable'] = 1;
		}
		return $this->find('all',$conditions);
	}

	/**
	 * 检测子节点
	 * @author zmx@gootop.net  
	 * @Date 2012-12-2
	 * @param
	 * @return 
	 */
	public function findSonNodel($parent_id = 0,$type = 'enable'){
		$conditions = array(
			'conditions'=>array(
				'parent_id'=>$parent_id
			)
		);
		if($type == 'enable'){
			$conditions['conditions']['Menu.enable'] = 1;
		}
		return $this->find('first',$conditions);
	} 
	
	/**
	 * 取权限菜单
	 * @author zmx@gootop.net  
	 * @Date 2012-12-3
	 * @param
	 * @return 
	 */
	public function getProwerMenu($parent_id = 0,$weight = 0){
		return $this->find('all',array(
			'conditions'=>array(
					'Menu.parent_id'=>$parent_id,
					'Menu.weight >='=>$weight,
					'Menu.enable'=>1
			),
			'order'=>'Menu.sort ASC,Menu.created ASC'
		));
	}
	
	/**
	 * 权限菜单树
	 * @author zmx@gootop.net  
	 * @Date 2012-12-3
	 * @param
	 * @return 
	 */
	public function getTreeProwerMenu($deep = 1,$weight = 0,$parent_id = 1){
		$deepTree = $this->getProwerMenu($parent_id,$weight);
		if($deep>1){
			$deep--;
			foreach ($deepTree as $lk => $level) {
				$sonDeepTree = $this->getTreeProwerMenu($deep,$weight,$level['Menu']['id']);
				$deepTree[$lk]['SonMenu'] = $sonDeepTree;
			}	
		}
		return $deepTree;
	}
	
	public function getTreePoint($controller,$action){
		return $this->find('first',array(
			'fields'=>array('Menu.id','Menu.parent_id','Menu.title'),
			'conditions'=>array(
				'Menu.controller'=>$controller,
				'Menu.action'=>$action
			)
		));
	}
	
	public function getBread($id){
		$bread = array(); 
		$tmp = $this->find('first',array(
				'conditions'=>array(
						'Menu.id'=>$id
				)
		));
		if($tmp && $tmp['Menu']['parent_id']>1){
			$tmp2 = $this->find('first',array(
					'conditions'=>array(
							'Menu.id'=>$tmp['Menu']['parent_id']
					)
			));
			if($tmp2['Menu']['parent_id']>1){
				$tmp3 = $this->find('first',array(
						'conditions'=>array(
								'Menu.id'=>$tmp2['Menu']['parent_id']
						)
				));
				$bread[0] = $tmp3;
			}
			$bread[1] = $tmp2;
		}
		$bread[2] = $tmp;
		return $bread;
	}
}
?>